<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/include/commonHeader::commonHeader('随机人员新增')"></head>
<body>
<div class="wrapper wrapper-content">
    <div class="col-sm-12">
        <form class="form-horizontal" id="exampleForm">
            <input id="cName" name="cName" type="hidden">
            <input id="flightIdList" name="flightIdList" type="hidden">
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-3 control-label">航班日期：</label>
                        <div class="col-xs-8">
                            <input id="flightDate" name="flightDate" class="form-control" type="text"
                                   placeholder="请选择日期">
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-3 control-label">随机人员：</label>
                        <div class="col-xs-8">
                            <!--<input id="cName" name="cName" class="form-control" type="text">-->
                            <select id="workcode" name="workcode"></select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-3 control-label">随机原因：</label>
                        <div class="col-xs-8">
                            <!-- <input id="sType" name="sType" class="form-control" type="text">-->
                            <select id="sType" name="sType"></select>
                        </div>
                    </div>
                </div>
                <div class="col-xs-3">
                    <div class="form-group">
                        <div class="col-xs-12">
                            <div class="checkbox">
                                <label>
                                    <input id="stats" name="stats" type="checkbox">控座
                                </label>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <label>
                                    <input id="payYn" name="payYn" type="checkbox">计费
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-3 control-label">机组联线：</label>
                        <div class="col-xs-8">
                            <select id="crewLinkLine" name="crewLinkLine"></select>
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <table id="flightTable" data-classes="table table-hover table-condensed">
                <thead>
                <tr>
                </tr>
                </thead>
            </table>
            <br>
            <div class="form-group">
                <div class="text-center">
                    <button id="btn_save" onclick="save()" type="button" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>

    </div>
</div>

<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
<script src="/plugins/select2/js/select2.min.js"></script>
<!-- 自定义js -->
<script th:inline="javascript">
    var prefix = "/additionalCrew";
    var flightDate = '';
    var moduleFlag = [[${moduleFlag}]];
    $(function () {
        $.fn.select2.defaults.set("theme", "bootstrap"); //使用select2-bootstrap主题
        $("#flightDate").datetimepicker({
            minView: "month",
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            autoclose: true,
            todayBtn: true,
            bootcssVer: 3,
            todayHighlight: true,
            pickerPosition: "bottom-right"
        }).on("change", function () {
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('flightDate', 'NOT_VALIDATED', null)
                .validateField('flightDate');
            flightDate = $("#flightDate").val();
            var linkLineData = [];
            if (flightDate) {
                linkLineData = getAjaxData("/select2/getCrewLinkLine", {flightDate: flightDate});
            }
            initCrewLinkLine(linkLineData);
        });
        initCrewLinkLine();
        //人员下拉初始化
        var paramArr = [{'moduleFlag':moduleFlag}];
        select2CommonAsync("#workcode","/select2/getStaff.do","人员姓名/首字母",null,paramArr);

        $("#sType").select2({
            minimumResultsForSearch: Infinity,
            data: getAjaxData("/select2/getCodeType", {parentCode: 'G1'}),
            placeholder: '请选择',
            width: '100%',
            allowClear: true,
        });
        $("#sType").val(null).trigger("change");

        initValidator();
        initFlightTable();
    });


    //保存
    function save() {
        $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
        if (!$("#exampleForm").data('bootstrapValidator').isValid()) {//判断验证是否已经通过
            return;
        }
        var selectFlight = $("#flightTable").bootstrapTable("getAllSelections");
        var flightIds = '';
        if (selectFlight && selectFlight.length == 0) {
            if (!$("#toast-container")[0]) {
                toastr.warning("请选择随机航班！");
            }
            return false;
        } else {
            for (var i = 0; i < selectFlight.length; i++) {
                flightIds += selectFlight[i].flightId + ",";
            }
            flightIds = flightIds.substr(0, flightIds.length - 1);
        }
        var stats = $("#stats").prop("checked");
        if (stats) {
            $("#stats").val("S");
        } else {
            $("#stats").val(null);
        }
        var payYn = $("#payYn").prop("checked");
        if (payYn) {
            $("#payYn").val("Y");
        } else {
            $("#payYn").val("N");
        }
        $("#cName").val($("#workcode option:selected").text());
        $("#flightIdList").val(flightIds);
        var data = {
            cName: $("#cName").val(),
            flightIdList: flightIds,
            flightDate: $("#flightDate").val(),
            workcode: $("#workcode").val(),
            sType: $("#sType").val(),
            stats: $("#stats").val(),
            payYn: $("#payYn").val(),
            crewLinkLine: $("#crewLinkLine").val(),

        };
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save.do",
            data: data,
            async: false,
            error: function (request) {
                toastr.error('数据出错！');
            },
            success: function (data) {
                if (data.code == 0) {
                    parent.toastr.success("操作成功");
                    parent.reload();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    parent.toastr.error(data.message);
                }
            }
        });
    }

    //初始化验证规则
    function initValidator() {
        $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                flightDate: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        }
                    }
                },
                workcode: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        }
                    }
                },
                sType: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        }
                    }
                },
                crewLinkLine: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        }
                    }
                }
            }
        });
    }

    function initFlightTable() {
        $('#flightTable').bootstrapTable(
            {
                method: 'post',
                striped: true,
                dataType: "json",
                clickToSelect: true,
                showColumns: false,
                contentType: "application/x-www-form-urlencoded",
                onDblClickRow: function (row, $element, field) {
                    view(row.id);
                },
                queryParams: function (params) {
                    return {
                        flightDate: $("#flightDate").val(),
                        crewLinkLineNo: $("#crewLinkLine").val()
                    };
                },
                columns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'flightNo',
                        title: '航班号'
                    },
                    {
                        field: '_departureAirport',
                        title: '发站'
                    },
                    {
                        field: '_arrivalAirport',
                        title: '到站'
                    },
                    {
                        field: 'std',
                        title: '始时'
                    },
                    {
                        field: 'sta',
                        title: '止时'
                    },
                ],
                onLoadSuccess: function (data) {
                    $('#flightTable').bootstrapTable("checkAll");
                }
            })
    }

    //
    function getAjaxData(reqUrl, params) {
        var result = [];
        $.ajax({
            url: reqUrl,
            dataType: 'json',
            data: params,
            method: 'post',
            cache: false,
            async: false,
            success: function (data) {
                result = data;
            },
            error: function (data) {
            }
        });
        return result;
    }

    function initCrewLinkLine(data) {
        if ($('#crewLinkLine').hasClass("select2-hidden-accessible")) {
            $("#crewLinkLine").select2("destroy").empty();
        }
        $("#crewLinkLine").select2({
            minimumResultsForSearch: Infinity,
            data: data,
            placeholder: '请选择',
            width: '100%',
            allowClear: true,
            tags: true,
            language: {
                noResults: function (e) {
                    return "未找到结果";
                }
            },
        }).on("change.select2", function () {
            if ($("#crewLinkLine").val()) {
                $("#flightTable").bootstrapTable("refresh", {url: prefix + "/getflightInfo"});
            } else {
                $("#flightTable").bootstrapTable("removeAll");
            }
        });
        $("#crewLinkLine").val(null).trigger("change");
    }
</script>
</body>
</html>
